package com.yuyou.train.user.service.impl;

import java.util.Date;
import java.util.List;

import com.yuyou.train.user.vo.resp.RespUserLookTop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyou.train.service.api.user.entity.UserLook;
import com.yuyou.train.user.mapper.UserLookMapper;
import com.yuyou.train.user.service.IUserLookService;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class UserLookServiceImpl implements IUserLookService{

	@Autowired
	private UserLookMapper userLookMapper;
	
	@Override
	public int deleteByPrimaryKey(String id) {
		return userLookMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(UserLook record) {
		record.setId(Sequence.getSequence().getNextId());
		record.setCreateTime(new Date());
		return userLookMapper.insert(record);
	}

	@Override
	public UserLook selectByPrimaryKey(String id) {
		return userLookMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(UserLook record) {
		record.setUpdateTime(new Date());
		return userLookMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public UserLook selectByForeignId(String foreignId) {
		return userLookMapper.selectByForeignId(foreignId);
	}

	@Override
	public int addLook(String foreignId, String resourceType) {		
		UserLook userLook=this.selectByForeignId(foreignId);
		if(userLook==null){
			userLook=new UserLook();
			userLook.setForeignId(foreignId);
			userLook.setResourceType(resourceType);
			userLook.setTime(Long.valueOf(1));			
			return this.insert(userLook);
		}else{
			userLook.setTime(userLook.getTime()+1);	
			return this.updateByPrimaryKeySelective(userLook);
		}

	}

	@Override
	public List<RespUserLookTop> findUserLookTop(String classId, String type) {
		return userLookMapper.findUserLookTop(classId, type);
	}

}